package com.networkviwer.swing;

import com.networkviwer.listeners.TableHeaderListener;
import com.networkviwer.networkviewer.NetworkViewer;
import com.networkviwer.utils.UnionDatasourceUtil;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description: 通用table，重写了构造方法
 * @author: GaleBlade
 * @date:
 */
public class CommonTable extends JTable {

    private JPopupMenu popupMenu;
    private int focusedRowIndex = -1;
    private int focusedColumnIndex = -1;

    /**
     * 构造方法
     */
    public CommonTable(Object[][] tableData, Object[] columnTitle) {
        super(tableData, columnTitle);
        //设置字体
        this.setFont(new Font("Menu.font", Font.PLAIN, 16));
        //设置行高
        this.setRowHeight(40);
        //设置table大小
        this.setPreferredScrollableViewportSize(new Dimension(1435,660));
        //设置颜色
        this.setBackground(new Color(67,67,67));
        this.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        this.setForeground(Color.WHITE);
        this.setGridColor(Color.WHITE);
        //添加右键菜单监听器
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseRightButtonClick(evt);
            }
        });
        //设置表头
        JTableHeader tableHeader = this.getTableHeader();
        //设置表头高度
        tableHeader.setPreferredSize(new Dimension(tableHeader.getWidth(), 35));
        //添加点击表头排序的监听器
        tableHeader.addMouseListener(new TableHeaderListener(this, tableHeader));
        tableHeader.setFont(new Font("Menu.font", Font.PLAIN, 16));
        tableHeader.setBackground(Color.GRAY);
//        tableHeader.setForeground(Color.WHITE);
        //创建右键菜单
        createPopupMenu();
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        int cache = getRowHeight() * getRowCount();
        return new Dimension(1435, Math.min(660, cache));
    }

    /**
     * 设置单元格不可编辑
     */
    public boolean isCellEditable(int row, int column) {return false;}

    /**
     * 设置右键菜单
     */
    private void createPopupMenu() {
        popupMenu = new JPopupMenu();
        JMenuItem copy = new JMenuItem();
        copy.setText("复制");
        copy.addActionListener(evt -> {
            //获取单元格的值并添加到剪贴板
            String value = this.getValueAt(focusedRowIndex, focusedColumnIndex).toString();
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(new StringSelection(value), null);
        });
        JMenuItem openFile = new JMenuItem();
        openFile.setText("打开文件所在位置");
        openFile.addActionListener(evt -> {
            //获取该进程的文件路径并打开资源管理器
            String pid = this.getValueAt(focusedRowIndex, 4).toString();
            try {
                String path = UnionDatasourceUtil.getProcessPath(pid);
                Runtime.getRuntime().exec("explorer.exe /e,/select," + path);
            } catch (IOException | SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        });
        JMenuItem killProcess = new JMenuItem();
        killProcess.setText("结束任务");
        killProcess.addActionListener(evt -> {
            //获取进程名并kill
            String name = this.getValueAt(focusedRowIndex, 5).toString();
            try {
                Runtime.getRuntime().exec("taskkill /im " + name + " /f");
                NetworkViewer.autoBtn.hold();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        popupMenu.add(copy);
        popupMenu.add(openFile);
        popupMenu.add(killProcess);
    }

    /**
     * 鼠标右键点击事件
     */
    private void mouseRightButtonClick(java.awt.event.MouseEvent evt) {
        //判断是否为鼠标的BUTTON3按钮，BUTTON3为鼠标右键
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            //通过点击位置找到点击为表格中的行
            focusedRowIndex = this.rowAtPoint(evt.getPoint());
            focusedColumnIndex = this.columnAtPoint(evt.getPoint());
            if (focusedRowIndex == -1) {
                return;
            }
            //将表格所选项设为当前右键点击的行
            this.setRowSelectionInterval(focusedRowIndex, focusedRowIndex);
            //弹出菜单
            popupMenu.show(this, evt.getX(), evt.getY());
        }
    }

}
